/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.action;

import com.myapp.hibernate.model.Usuario;
import com.myapp.hibernate.util.UsuarioUtil;
import com.myapp.struts.form.BorrarUsuarioForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author jiglrom
 */
public class BorrarUsuarioAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String NOBORRAR = "noBorrar";
    private static final String BORRAR = "borrar";
    private static final String INPUT = "init";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        BorrarUsuarioForm formulario = (BorrarUsuarioForm) form;
        String pass = formulario.getPass();
        Usuario usuario = (Usuario) request.getSession().getAttribute("UsuarioLogado");
        UsuarioUtil usuarioUtil = new UsuarioUtil();
        if (request.getParameter("borrar") != null) {
            if (usuario.getPass().equals(pass)) {
                usuarioUtil.borrar(usuario);
                request.getSession().setAttribute("informacion", "Usuario Borrado");
                request.getSession().removeAttribute("UsuarioLogado");
                return mapping.findForward(BORRAR);
            } else {
                ActionErrors errors = new ActionErrors();
                errors.add("oldPass", new ActionMessage("error.incorrecta"));
                saveErrors(request, errors);
                return mapping.findForward(INPUT);
            }

        } else if (request.getParameter("noBorrar") != null) {
            request.getSession().removeAttribute("borrarUsuario");
            return mapping.findForward(NOBORRAR);
        }
        return mapping.findForward(INPUT);
    }
}
